// 将路由导航守卫从router文件中抽离，以便后续的权限管理，不同权限的人映射不同的组件
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

NProgress.configure({ showSpinner: false }) // NProgress Configuration

// 白名单
const whiteList = ['/login'] // no redirect whitelist

// 路由前置导航守卫
router.beforeEach(async(to, from, next) => {
  // 开始进度条
  NProgress.start()
  const token = store.getters.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done() // 解决手动输入地址进度条不关闭的场景
    } else {
      console.log(111)
      if (!store.getters.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done() // 解决手动输入地址进度条不关闭的场景
    }
  }
})

// 路由后置导航守卫
router.afterEach(() => {
  // 结束进度条
  NProgress.done()
})
